package com.test.jdk;

/**
 * Created by houjinxin on 16/9/26.
 */
public class FinalizeEscapeGC {

    private static FinalizeEscapeGC SAVE_HOOK = null;

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize method executed");
        FinalizeEscapeGC.SAVE_HOOK = this;
    }

    public static void main(String[] args) throws InterruptedException {
        SAVE_HOOK = new FinalizeEscapeGC();
        SAVE_HOOK = null;
        System.gc();
        Thread.sleep(500);
        if (SAVE_HOOK != null){
            SAVE_HOOK.isAlive();
        } else {
            System.out.println("no i am dead");
        }

//        SAVE_HOOK = null;
//        System.gc();
//        Thread.sleep(500);
//        if (SAVE_HOOK != null){
//            SAVE_HOOK.isAlive();
//        } else {
//            System.out.println("no i am dead");
//        }

    }

    private void isAlive() {
        System.out.println("yes i am still alive");
    }
}
